﻿if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(LocationOK);
}
else {
    SearchList("", "", "", 1, 10);
//    var distances = document.getElementsByName("distance");
//    for (var i=0; i < distances.length; i++)
//    {
//        distances[i].disabled = true;
//    }
}
//LocationOK();
function LocationOK(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    //latitude = 48.83526003;
    //longitude = 2.12060213;
    document.getElementById("latit").value = latitude;
    document.getElementById("longit").value = longitude;

    SearchList("",latitude, longitude, 1, 2);
}
function get_distance_value(distance)
{
for (var i=0; i < distance.length; i++)
   {
   if (distance[i].checked)
      {
      return distance[i].value;
      }
   }
}

function SubmitSearch() {
    document.getElementById('search_list').innerHTML = "";
    SearchList(document.getElementById("SearchText").value, document.getElementById("latit").value, document.getElementById("longit").value, get_distance_value(document.getElementsByName("distance")), 10);
}
var xhr_object;
function SearchList(searchtext,latitude,longitude,distance,nb) {
    xhr_object = new XMLHttpRequest();
    xhr_object.open("POST", "/Home/TwittList", true);
    xhr_object.onreadystatechange = GetList;
   
    var data = "searchtext=" + searchtext + "&latit=" + latitude + "&longit=" + longitude +"&distance="+distance+"&nb="+nb;
    xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr_object.send(data);
}
function NextList(next) {
    xhr_object = new XMLHttpRequest();
    xhr_object.open("POST", "/Home/TwittListNext", true);
    xhr_object.onreadystatechange = GetList;
    var data = "next=" + next;
    xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr_object.send(data);
}

 function GetList() {
     if (xhr_object.readyState == 4) {
        if(document.getElementById('more'))
            document.getElementById('more').outerHTML = "";
            document.getElementById('search_list').innerHTML += xhr_object.responseText;
            //if (nb < 10)
            
       }
}